import { BasicColumn } from '/@/components/Table';
import { LIST_FIELD_TYPE, LIST_DISPLAY_TYPE } from '../types';
import { isNullOrUnDef, isObject } from '/@/utils/is';
import Link from '../components/Link.vue';
import Function from '../components/function/index.vue';
import ImageCom from '../components/img/index.vue';
import Round from '../components/label/Round.vue';
import Tag from '../components/label/Tag.vue';
import HSwitch from '../components/HSwitch.vue';
import { HFileReview } from '/@/components/HFileReview';

export default async function setColumn(titles: any[], showFields: any[]): Promise<BasicColumn[]> {
  const source = titles.filter((cl) =>
    [LIST_DISPLAY_TYPE.SHOW, LIST_DISPLAY_TYPE.DEF_SHOW].includes(cl.displayType),
  );
  const columns = columnsSorts(source, showFields);
  for (const [key, item] of Object.entries(columns)) {
    if (item.isSort === 1) {
      item.sorter = {
        compare: () => {},
        multiple: key + 1,
      };
      // item.sortDirections = [1, 0];
    }
    delField(item);

    parseFormat(item);
  }

  return columns;
}

function columnsSorts(source: any[], target: any[]): any[] {
  if (isNullOrUnDef(target)) return source;
  if (!target.length) {
    source.forEach((el) => {
      el.defaultHidden = true;
    });
    return source;
  }
  const arr: any[] = [];

  for (const item of target) {
    const sItem = source.find((el) => el.dataIndex === item.dataIndex);
    if (sItem) {
      sItem.defaultHidden = item.defaultHidden;
      arr.push(sItem);
    }
  }

  // 获取新字段
  const newFields = source.filter((el) => !target.find((item) => el.dataIndex === item.dataIndex));
  arr.push(...newFields);
  return arr;
}
const isFrontFunction = ({ fieldType }) => fieldType === LIST_FIELD_TYPE.FUNCTION_FIELD_TYPE;

const isLinkType = ({ fieldType }) => fieldType === LIST_FIELD_TYPE.LINK_FIELD_TYPE;
const isFileReviewType = ({ fieldType }) => fieldType === LIST_FIELD_TYPE.FILE_REVIEW_TYPE;
const isOptionsType = ({ fieldType }) =>
  [LIST_FIELD_TYPE.DICT_FIELD_TYPE, LIST_FIELD_TYPE.RELA_FIELD_TYPE].includes(fieldType);
const isConditionOptionType = ({ fieldType }) =>
  [LIST_FIELD_TYPE.OPTION_FIELD_TYPE, LIST_FIELD_TYPE.MIX_FIELD_TYPE].includes(fieldType);

const isImgType = ({ fieldType }) => [LIST_FIELD_TYPE.IMG_FIELD_TYPE].includes(fieldType);
const isSwitchType = ({ fieldType }) => [LIST_FIELD_TYPE.SWITCH_FIELD_TYPE].includes(fieldType);

function parseFormat(fieldInfo: any) {
  fieldInfo.format = (v, record) => {
    switch (true) {
      case isFrontFunction(fieldInfo): // 执行函数
        return <Function text={v} record={record} config={fieldInfo} />;

      case isOptionsType(fieldInfo) && !!fieldInfo.options: //  字典类型 并且 有选项时
        const info = fieldInfo.options.find((el) => Number(el.value) === Number(v));
        if (info) {
          return info.label;
        }
        return v;

      case isFileReviewType(fieldInfo): // 文件图象
        return v ? <HFileReview info={v} /> : '';
      case isLinkType(fieldInfo): // 链接解析
        if (isObject(v)) {
          return <Link info={v} />;
        }
        return v;
      case isImgType(fieldInfo): // 图像解析
        return <ImageCom url={v} config={fieldInfo} />;

      case isConditionOptionType(fieldInfo): // 分类值解析
        if (!isObject(v)) return v;
        if (v.labelType === 'round') {
          return <Round info={v} />;
        }

        if (v.labelType === 'tag') {
          return <Tag info={v} />;
        }
        return v.label;

      case isSwitchType(fieldInfo): // 开关解析
        const handleChange = (v) => {
          record[fieldInfo.dataIndex] = v;
        };
        return <HSwitch info={fieldInfo} value={record} onChange={handleChange} />;

      default:
        return v;
    }
  };
}

function delField(fieldInfo: Recordable) {
  const delKeys = ['width', 'fixed', 'colSpan'];

  for (const key of delKeys) {
    if (isNullOrUnDef(fieldInfo[key])) {
      delete fieldInfo[key];
    }
  }
}
